import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QIcon
from universal.custom.ini_file import ODvision_setting,ODvision_config
import hashlib
def encoding_(inputPassword):
    inputPassword = hashlib.md5(inputPassword.encode())
    inputPassword_hx = inputPassword.hexdigest()
    return inputPassword_hx

class codeWidget(QTabWidget):
    exit_Odvision = pyqtSignal(bool)
    exit_FtpUpload = pyqtSignal(bool)

    def __init__(self, w):
        super(codeWidget,self).__init__()
        # self.TabWidget = QTabWidget()
        self.init_page1() #第一行
        self.init_page2()
        self.addTab(self.codeWidget, "退出")
        self.addTab(self.widget_code, "修改密码")
        self.status_ = w

    def init_page1(self):
        self.codeWidget = QWidget()
        self.codeWidget_layout = QVBoxLayout(self.codeWidget)
        #init_line1
        self.layout_1 = QHBoxLayout()
        self.Code_label = QLabel('请输入退出密码:')


        self.Code_edit = QLineEdit()
        self.Code_edit.setEchoMode(QLineEdit.Password)
        self.CodeStatus = True
        act = QAction(self)
        act.setIcon(QIcon(':/icons/1-1.ico'))  # 待修改
        act.triggered.connect(lambda: self.showPassword())
        self.Code_edit.addAction(act, QLineEdit.TrailingPosition)
        self.layout_1.addWidget(self.Code_label)
        self.layout_1.addWidget(self.Code_edit)

        #init_line2
        self.layout_2 = QHBoxLayout()
        self.OK = QPushButton('确认')
        self.OK.clicked.connect(self.OK_quitOD)
        self.Exit = QPushButton('取消')
        self.Exit.clicked.connect(self.quit)
        self.layout_2.addWidget(self.OK)
        self.layout_2.addWidget(self.Exit)

        self.codeWidget_layout.addLayout(self.layout_1)
        self.codeWidget_layout.addLayout(self.layout_2)

    def showPassword(self):
        if self.CodeStatus == True:
            self.Code_edit.setEchoMode(QLineEdit.Normal)
            self.CodeStatus = False
        else:
            self.Code_edit.setEchoMode(QLineEdit.Password)
            self.CodeStatus = True

    def OK_quitOD(self):
        input_code = self.Code_edit.text()
        input_code = encoding_(input_code)
        nowPassword = ODvision_config.read_value('settings', 'password')
        if input_code == nowPassword:
            if self.status_ == 'main':
                self.exit_Odvision.emit(True)
            if self.status_ == 'ftp':
                self.exit_FtpUpload.emit(True)

    def clearLine(self):
        self.Code_edit.clear()
        self.Code_edit_pre.clear()
        self.Code_edit_now.clear()

    def quit(self):
        self.close()

    def exit(self):
        self.clear()
        self.deleteLater()

    def init_page2(self):
        self.widget_code = QWidget()
        self.layout_code = QVBoxLayout(self.widget_code)
        codeLayout_pre = QHBoxLayout()
        self.Code_label_pre = QLabel('请输入原密码:')
        self.Code_edit_pre = QLineEdit()
        self.Code_edit_pre.setEchoMode(QLineEdit.Password)
        codeLayout_pre.addWidget(self.Code_label_pre)
        codeLayout_pre.addWidget(self.Code_edit_pre)
        codeLayout_now = QHBoxLayout()
        self.Code_label_now = QLabel('请输入现密码:')
        self.Code_edit_now = QLineEdit()
        self.Code_edit_now.setEchoMode(QLineEdit.Password)
        codeLayout_now.addWidget(self.Code_label_now)
        codeLayout_now.addWidget(self.Code_edit_now)
        self.Code_change = QPushButton('确认修改')
        self.Code_change.clicked.connect(self.changeCode)
        self.Textbrowser = QTextBrowser()
        self.Textbrowser.setFixedHeight(70)
        # self.Code_change.clicked.connect()
        self.layout_code.addLayout(codeLayout_pre)
        self.layout_code.addLayout(codeLayout_now)
        self.layout_code.addWidget(self.Code_change)
        self.layout_code.addWidget(self.Textbrowser)

    def changeCode(self):
        input_code = self.Code_edit_pre.text()
        input_code = encoding_(input_code)
        nowPassword = ODvision_config.read_value('settings', 'password')
        Now_code = self.Code_edit_now.text()
        if input_code == nowPassword:
            self.Textbrowser.clear()
            self.Textbrowser.append('密码修改成功!')
            Now_code = encoding_(Now_code)
            ODvision_config.write_value('settings', 'password', Now_code)
        else:
            self.Textbrowser.append('原密码错误!')




if __name__ == '__main__':
    app =QApplication(sys.argv)
    w = codeWidget()
    w.show()
    sys.exit(app.exec())
